package com.wanwanbear.www.service.impl;

import com.sun.xml.internal.xsom.impl.Ref;
import com.wanwanbear.www.service.UploadFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by wz on 2017/6/26.
 */
@Service
public class ImplUploadFile implements UploadFile {
    public boolean upload(MultipartFile upload) {
        if(!upload.isEmpty()){
            try {
                File file = new File("C://image/my/");
                if(!file.exists()){
                    file.mkdir();
                }
                byte[] bytes = upload.getBytes();
                BufferedOutputStream bufferedOutputStream;
                if(upload.getContentType().equals("image/jpeg")) {
                    bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(file.getPath()+"music.jpg" )));
                }else if(upload.getContentType().equals("audio/amr")){
                    bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(file.getPath()+"music.amr" )));
                }
                else{
                    bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(file.getPath()+"music" )));

                }
                bufferedOutputStream.write(bytes);
                bufferedOutputStream.close();
                return true;

            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }
}
